package com.cssw.fyzb.busi.api.notify;

import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.core.model.vo.notify.NotifyVo;
import com.cssw.fyzb.busi.service.user.UserRelService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.busi.service.video.NotifyService;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.eos.NotifyEo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Jian Qin.
 */
@Function(description = "查询粉我的消息", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识"),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
}, result = @Result(
        properties = {
                @Property(name = "total", type = Type.Integer, description = "总页数"),
                @Property(name = "count", type = Type.Integer, description = "总行数"),
        },
        listProperties = {
                @ListProperty(name = "list", properties = {
                        @Property(name = "userId", type = Type.Integer, description = "作者编号"),
                        @Property(name = "nickName", type = Type.String, description = "作者昵称"),
                        @Property(name = "avatar", type = Type.String, description = "作者头像"),
                        @Property(name = "vedioId", type = Type.Integer, description = "视频编号"),
                        @Property(name = "thumbs", type = Type.String, description = "视频封面"),
                        @Property(name = "content", type = Type.String, description = "消息内容"),
                        @Property(name = "tips", type = Type.String, description = "提示类容"),
                        @Property(name = "createTime", type = Type.Integer, description = "创建时间"),
                })
        }
))
@Component
public class ListFocus implements Action {
    @Autowired
    private NotifyService notifyService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRelService userRelService;


    private static final Logger logger = LoggerFactory.getLogger(ListAt.class);
    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        Integer pageNumber = request.getInteger("pageNumber");
        Integer pageSize = request.getInteger("pageSize");
        try {
            Page<NotifyEo> page = notifyService.pageFocusByUserId(userId, pageNumber, pageSize);
            Map<String, User> userMap = new HashMap<>();
            List<NotifyVo> list = ((Converter<List<NotifyEo>, List<NotifyVo>>) notifyVos -> {
                List<NotifyVo> vos = new ArrayList<>();
                notifyVos.forEach((v) -> {
                    NotifyVo vo = new NotifyVo();
                    vo.setContent(v.getContent());
                    vo.setCreateTime(v.getCreateTime());
                    vo.setThumbs(v.getThumbs());
                    vo.setTips(v.getTips());
                    vo.setUserId(v.getUserId());
                    vo.setVideoId(v.getVideoId());

                    User user = userMap.get(String.valueOf(v.getUserId().intValue()));
                    if(user == null){
                        user = userService.getById(v.getUserId());
                        userMap.put(String.valueOf(user.getUserId().intValue()), user);
                    }

                    Long relType = userRelService.getRelType(v.getUserId(), userId);
                    vo.setRelType(relType);

                    vo.setAvatar(user.getAvatar());
                    vo.setNickName(user.getNickName());
                    vos.add(vo);
                });
                return vos;
            }).convert(page.getList());
            return Response.success().put("list", list).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }
}
